﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace Payton.WP7.Common.Model
{
    public class SNSProviderManager
    {
        private static readonly SNSProviderManager _current = new SNSProviderManager();
        public static SNSProviderManager Current
        {
            get { return _current; }
        }

        Dictionary<SNSProviderType, SNSProvider> _dic = new Dictionary<SNSProviderType, SNSProvider>();

        private SNSProviderManager()
        {
            Load();
        }


        private void Load()
        {
            //TODO 将isolated storage中保存的provider信息写入到_dic
        }

        public SNSProvider GetProvider(SNSProviderType pType)
        {
            if (!_dic.ContainsKey(pType))
            {
                lock (_dic)
                {
                    var factory = SNSProviderFactory.GetFactory(pType);
                    if (factory == null)
                    {
                        return null;
                    }
                    var provider = factory.GetProvider();

                    if (provider != null)
                    {
                        _dic.Add(pType, provider);
                    }
                }
            }
            return _dic[pType];
        }

        public IEnumerable<SNSProvider> GetAllSupportedProviders()
        {
            List<SNSProvider> all = new List<SNSProvider>();
            foreach (var p in EnumHelper.GetValues<SNSProviderType>())
            {
                var provider = GetProvider(p);
                if (provider != null && provider.IsSupported)
                    all.Add(provider);
            }
            return all;
        }
    }
}
